package org.e_gold.util;

import java.text.DecimalFormat;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class CalcUtil {

	private static final Logger logger = Logger.getLogger(CalcUtil.class);

	public static int randomTime() {

		int min = 1;
		int max = 10;
		return randomTime(min, max);
	}

	public static int randomTime(int min, int max) {

		return (int) ((Math.random() * (max - min)) + min);
	}

	public static Integer getTotalGoal(String text) {

		int homeGoal = Integer.parseInt(text.split("-")[0]);
		int awayGoal = Integer.parseInt(text.split("-")[1]);
		return (homeGoal + awayGoal);
	}

	public static Integer getCurrentTime(String text) {

		if (StringUtils.isEmpty(text) || text.length() != 5)
			return 0;
		return Integer.parseInt(text.split(":")[0]);
	}

	public static Float getRate(String contractStr) {

		Float rate = 0f;
		String rateStr = contractStr.split(" ")[0];
		if (!StringUtils.isEmpty(rateStr) && rateStr.indexOf("/") > -1) {
			Float first = Float.parseFloat(rateStr.split("/")[0]);
			Float last = Float.parseFloat(rateStr.split("/")[1]);
			rate = first / last;
			return rate + 1;
		} else {
			return Float.parseFloat(rateStr);
		}
	}

	// "Under 7.5" -> 8
	public static int getUnderTotalGoal(String contractType) {

		String under = contractType.split(" ")[1];
		try {
			return Math.round(Float.parseFloat(under));
		} catch (Exception e) {
			logger.info("getUnderTotalGoal error : " + contractType);
			e.printStackTrace();
		}
		return 0;
	}

	// ex : 79398.55 USD
	public static Float convertBalance(String balanceStr) {

		balanceStr = balanceStr.split(" ")[0];
		return Float.parseFloat(balanceStr);
	}

	public static Float routingBalance(String balanceStr){
		Float dbl = Float.parseFloat(balanceStr);

		return rountingBalance(dbl);
	}

	public static Float rountingBalance(Float dbl){

		DecimalFormat df = new DecimalFormat("##0.00");
		String str = df.format(dbl);

		return Float.parseFloat(str);
	}
}
